package com.tanxi521.com.thenorthtitle.util;

import com.tanxi521.com.thenorthtitle.TheNorthTitle;
import com.tanxi521.com.thenorthtitle.entity.PlayerTitle;
import com.tanxi521.com.thenorthtitle.sql.dao.TnTitleBaseInfoMapper;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class GuiUtil {

    public final static String TITLE_NAME = "%title_player_name%";
    public final static String TITLE_PRICE = "%title_player_price%";
    public final static String TITLE_PRE = "%title_player_pre%";
    public final static String TITLE_DISCOUNT = "%title_player_discount%";

    public static boolean openSelectGui(Player player) {

        try {
            Inventory inventory = Bukkit.createInventory(player, 9, "§e§l北境称号");
            // 分隔
            ItemStack blueGlass = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 11);
            ItemMeta itemMeta = blueGlass.getItemMeta();
            itemMeta.setDisplayName("§e北境称号§r-§6分隔板");
            blueGlass.setItemMeta(itemMeta);
            // 我的称号
            ItemStack myTitle = new ItemStack(Material.ENDER_CHEST, 1);
            ItemMeta myTitleItemMeta = myTitle.getItemMeta();
            myTitleItemMeta.setDisplayName("§e我的称号");
            LinkedList<String> lore = new LinkedList<>();
            lore.add("§2当前信息:");
            lore.add("§e称号: " + PlaceholderAPI.setPlaceholders(player, TITLE_NAME));
            lore.add("§3前置称号: " + PlaceholderAPI.setPlaceholders(player, TITLE_PRE));
            lore.add("§b价值: " + PlaceholderAPI.setPlaceholders(player, TITLE_PRICE) + " §e金币");
            lore.add("§6系统商店金币折扣: §c" + PlaceholderAPI.setPlaceholders(player, TITLE_DISCOUNT));
            lore.add("§c点击查看全部");
            myTitleItemMeta.setLore(lore);
            myTitle.setItemMeta(myTitleItemMeta);
            // 称号商店
            ItemStack titleStore = new ItemStack(Material.ENDER_CHEST, 1);
            ItemMeta storeItemMeta = titleStore.getItemMeta();
            storeItemMeta.setDisplayName("§6称号商店");
            titleStore.setItemMeta(storeItemMeta);
            // 称号每日奖励
            ItemStack dailyReward = new ItemStack(Material.ENDER_CHEST, 1);
            ItemMeta dailyRewardItemMeta = dailyReward.getItemMeta();
            dailyRewardItemMeta.setDisplayName("§e§l称§b§l号§c§l定§6§l制");
            dailyRewardItemMeta.setLore(Collections.singletonList("§b§l1000钻石/个"));
            dailyReward.setItemMeta(dailyRewardItemMeta);

            inventory.setItem(0, blueGlass);
            inventory.setItem(1, myTitle);
            inventory.setItem(2, blueGlass);
            inventory.setItem(3, blueGlass);
            inventory.setItem(4, titleStore);
            inventory.setItem(5, blueGlass);
            inventory.setItem(6, blueGlass);
            inventory.setItem(7, dailyReward);
            inventory.setItem(8, blueGlass);

            player.openInventory(inventory);
        } catch (Exception e) {
            TheNorthTitle.getInstance().getLogger().warning("渲染称号主页gui错误");
            player.sendMessage("§c插件出错，请联系服主");
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean openTitleGui(Player player, Integer page, Integer type) {

        try {
            int pageSize = 28;
            List<PlayerTitle> userTitles = TnTitleBaseInfoMapper.queryPlayerTitleByPage(player, page == 1 ? 0 : (page - 1) * pageSize, pageSize, type);
            Inventory titleInventory = createTitleInventory(player, type == 1 ? "§2§l我的称号 第" + page + "页" : "§a§l称号商店 第" + page + "页");
            int index = 0;
            for (int row = 1; row < 5; row++) {
                for (int column = 1; column < 8; column++) {
                    if (index + 1 <= userTitles.size()) {
                        int slot = row * 9 + column;
                        PlayerTitle playerTitle = userTitles.get(index);
                        ItemStack titleSlot = new ItemStack(Material.NAME_TAG, 1);
                        ItemMeta slotItemMeta = titleSlot.getItemMeta();
                        slotItemMeta.setDisplayName(playerTitle.getTitleName());
                        LinkedList<String> lore = new LinkedList<>();
                        lore.add("§b称号ID: " + playerTitle.getId());
                        lore.add(playerTitle.getTitlePre());
                        lore.add(playerTitle.getPrice());
                        lore.add(playerTitle.getTitleDisCount());
                        // lore.add(playerTitle.getTitleRewardMul()); // 礼包倍率 以后加
                        lore.add(type == 1 ? "§c点击佩戴" : "§c点击购买");
                        slotItemMeta.setLore(lore);
                        titleSlot.setItemMeta(slotItemMeta);
                        titleInventory.setItem(slot, titleSlot);
                        index++;
                    } else break;
                }
            }
            player.openInventory(titleInventory);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static Inventory createTitleInventory(Player player, String inventoryName) {

        Inventory inventory = Bukkit.createInventory(player, 6 * 9, inventoryName);

        // 返回
        ItemStack back = new ItemStack(Material.PAPER, 1);
        ItemMeta backItemMeta = back.getItemMeta();
        backItemMeta.setDisplayName("§6返回");
        back.setItemMeta(backItemMeta);

        // 分隔
        ItemStack blueGlass = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 11);
        ItemMeta itemMeta = blueGlass.getItemMeta();
        itemMeta.setDisplayName("§e北境称号§r-§6分隔板");
        blueGlass.setItemMeta(itemMeta);

        // 上一页
        ItemStack up = new ItemStack(Material.INK_SACK, 1, (short) 9);
        ItemMeta upItemMeta = up.getItemMeta();
        upItemMeta.setDisplayName("§e上一页");
        up.setItemMeta(upItemMeta);

        // 下一页
        ItemStack down = new ItemStack(Material.INK_SACK, 1, (short) 10);
        ItemMeta downItemMeta = down.getItemMeta();
        downItemMeta.setDisplayName("§e下一页");
        down.setItemMeta(downItemMeta);

        inventory.setItem(0, back);
        inventory.setItem(45, up);
        inventory.setItem(53, down);
        for (int start = 1; start < 9; start++) inventory.setItem(start, blueGlass);
        for (int start = 9; start < 45; start += 9) inventory.setItem(start, blueGlass);
        for (int start = 8; start < 53; start += 9) inventory.setItem(start, blueGlass);
        for (int start = 46; start < 53; start++) inventory.setItem(start, blueGlass);

        return inventory;
    }

}
